#include "Llamadas.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>

#define SHM_PERMISOS 0666
#define SHM_KEY_CACHE 107
#define SHM_KEY_STRUCT 108

BufferCache * cache; //ambas son para el sistema (mem compartida)
StructFile * structFile;

TDFU tdfu; //para este proceso

int main(int argc, char * argv[]) {
   //BufferCache
   int idcache = shmget(SHM_KEY_CACHE, sizeof(BufferCache), IPC_CREAT | SHM_PERMISOS); //crea memoria compartida
   if (idcache < 0) {
      fprintf(stderr, "cache shmget failed: %s\n", strerror (errno));
      exit(1);
   }
  
   cache = (BufferCache *) shmat(idcache, (char *) 0, 0); //dato apunta a memoria
   if (cache < (BufferCache *) 0) {
      fprintf (stderr, "cache shmat failed: %s\n", strerror(errno));
      exit(1);
   }  
   

   //Struct File
   int idstruct = shmget(SHM_KEY_STRUCT, sizeof(StructFile), IPC_CREAT | SHM_PERMISOS); //crea memoria compartida
   if (idstruct < 0) {
      fprintf(stderr, "struct file shmget failed: %s\n", strerror (errno));
      exit(1);
   }
  
   structFile = (StructFile *) shmat(idstruct, (char *) 0, 0); //dato apunta a memoria
   if (structFile < (StructFile *) 0) {
      fprintf (stderr, "struct file shmat failed: %s\n", strerror(errno));
      exit(1);
   }

   //*********************************************************************************
   captura_usr1_usr2(); //captura senales SIGUSR1 Y SIGUSR2
   tdfu_init();

   //***************** "Codigo de usuario" ********************************************



   //***************** Prueba 0.1 *****************************************************
   /*printf("\nLee cache.freeStart: %d \n", cache->freeStart);
   printf("freeEnd = %d \n", cache->freeEnd);
   tdfu.tam = getpid();
   printf("tdfu.tam = %d\n", tdfu.tam);
   printf("struct file.tam = %d\n", structFile->tam); */


   //***************** Prueba 0.2 *****************************************************
   /*char * cadena = new char [4];
   cadena = "abcd";
   char * cadena2;
   cadena2 = cadena;
   cadena2++;
   printf("cadena = %s, cadena2 = %s \n", cadena, cadena2);

   char * mem = new char [5];
   mem[0] = '1';
   mem[1] = '2';
   mem[2] = '3';
   mem[3] = '4';
   mem[4] = '5';
   //mem = "1234";
   //mem++;
   printf("mem = %s \n", mem);
   memcpy(mem + 1, cadena2, 2); //mem <- cadena2  (tam + \0)
   //mem--;
   printf("despues de segundo memcpy\n");
   printf("mem = %s \n", mem);

   printf("12 mod 5 = %d \n", 12 % 5); */

   //***************** Prueba 0.3 *****************************************************
   /*int fd = open("Ficheritos/in2.txt", O_WRONLY | O_CREAT | O_EXCL, 0777);
   if (fd < 0) {
      printf("Error open\n");
      return 0;
   }
   char * buf = "ASDF";
   write(fd, buf, 3);
   if (close(fd) < 0)
      printf("Error en close\n");*/


   //***************** Prueba 1: BufferCache ******************************************
   /* bc_insertHash(1, 20, 30);
   bc_insertHash(1, 10, 40);
   bc_insertHash(1, 10, 41);
   bc_insertHash(1, 10, 42);
   printf("Despues de insertar:\n");
   //bc_extractHash(0);
   //bc_extractFree(11);
   bc_insertHash(1, 10, 43);
   bc_insertHash(1, 10, 50);
   bc_insertHash(1, 10, 80);

   printf("\nfound = %d", bc_findHash(1, 10, 41));
   printf("\nfound = %d\n", bc_findHash(20, 10, 41));

   bc_print();

   //bc_extractHash(5);
   
   bc_insertFreeFirst(3);
   bc_insertFreeFirst(4);
   bc_print(); */


   //***************** Prueba 2: Rutinas **********************************************

   //int nbuf = getblk(1, 10, 40);
   //printf("nbuf = %d", nbuf);

   int sem = semAt(SEM_ID_CACHE); //coge semaforo (mem compartida)

   int arg = atoi(argv[1]);
   if (arg == 1) {
      printf("antes de wait\n");
      semWait(sem);
      printf("despues de wait 1\n");
   }
   else {
      printf("antes de signal\n");
      semSignal(sem);
      printf("despues de signal\n");
   }

   //semSignal(sem);
   //perror("error prueba\n"); //el perror es solo un print asi q lo venga despues si sale
   //printf("despues de perror\n"); 

   //***************** Prueba 3: Llamadas *********************************************
   /* int fd1 = abrir("Ficheritos/in3.txt", O_RW);
   char * buf = "bbbb";
   posicionar(fd1, 5, 0);
   escribir(fd1, buf, 3);
   buf = "zzzz";
   escribir(fd1, buf, 3);
   cerrar(fd1); */
 
}

